import javafx.application.Application;
import javafx.event.ActionEvent;
import javafx.fxml.FXML;
import javafx.fxml.FXMLLoader;
import javafx.scene.Parent;
import javafx.scene.Scene;
import javafx.scene.control.Button;
import javafx.scene.control.ListView;
import javafx.scene.control.TextArea;
import javafx.scene.control.TextField;
import javafx.stage.Stage;
import javafx.fxml.Initializable;
import java.net.URL;
import java.util.ResourceBundle;

public class MainWindow extends Application implements Initializable{

    public static DmjMain dmjMain;
    @FXML
    TextArea logText;
    @FXML
    TextField roomidText;
    @FXML
    Button linkBtn;
    @FXML
    Button cleanBut;
    @FXML
    ListView modularList;
    @Override
    public void start(Stage primaryStage) throws Exception{
        Parent root = FXMLLoader.load(getClass().getResource("sample.fxml"));
        primaryStage.setTitle("Hello World");
        primaryStage.setScene(new Scene(root, 600, 400));
        primaryStage.setOnHiding( event -> {
            System.exit(0);
        } );
        primaryStage.show();
    }

    @Override
     public void initialize(URL location, ResourceBundle resources) {
        // TODO Auto-generated method stub
        dmjMain.logText = logText;
        dmjMain.roomidText = roomidText;
        dmjMain.linkBtn = linkBtn;
        dmjMain.cleanBut = cleanBut;
        dmjMain.modularList = modularList;
        for (Imodular imodular : dmjMain.imodulars){
            modularList.getItems().add(imodular.modularName()+imodular.version());
        }
    }

    @FXML
    public void linkBtn_click(ActionEvent e){
        String roomid = roomidText.getText();
        dmjMain.rundm(roomid);
        logText.appendText(roomid+"\n");
    }

    @FXML
    public void setBut_click(ActionEvent e){
        if (modularList.getSelectionModel().getSelectedIndex() == -1){
            logText.appendText("请选择模组\n");
            return;
        }
        dmjMain.imodulars.get(modularList.getSelectionModel().getSelectedIndex()).Setting();
    }

    public void setDmjMain(DmjMain dmjMain1){
        dmjMain = dmjMain1;
    }

    public static void main(String[] args) {
        launch(args);
    }

}